package com.aspect.aspect.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/10/25 17:50
 */

@Aspect
@Component
public class FirstAspect implements Ordered {
    @Autowired
    private ApplicationContext applicationContext;


    @Pointcut("@annotation(com.aspect.aspect.annotation.MyAnnotation)")
    public void point(){}
    @Around("point()")
    public Object invoke(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println("FirstAspect-------start");
        Object res=null;
        try {
            res= proceedingJoinPoint.proceed();

            Class c=proceedingJoinPoint.getTarget().getClass();
            Object[] args= proceedingJoinPoint.getArgs();
            MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
            Class<?>[] argsType=signature.getMethod().getParameterTypes();
            Object o=applicationContext.getBean(c);
            Method m=c.getDeclaredMethod("confirm",argsType);
            m.invoke(o,args);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("FirstAspect-------end");
        return res;
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
